//
//  VisitorView.swift
//  WB_Swift3.0
//
//  Created by wangrui on 2017/3/19.
//  Copyright © 2017年 wangrui. All rights reserved.
//

import UIKit

/// 访客视图
class VisitorView: UIView
{
    var visitorInfo:[String:String]? {
        didSet {
            setupInfo(dict: visitorInfo!)
        }
    }
    // 旋转图片
    fileprivate lazy var backIconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 小房子
    fileprivate lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 提示文字
    fileprivate lazy var tipLabel:UILabel = UILabel.cz_label(
        withText: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜",
        fontSize: 14,
        color: .darkGray)
    
    // 注册按钮
    lazy var registerButton:UIButton = UIButton.cz_textButton(
        "注册",
        fontSize: 16,
        normalColor: .orange,
        highlightedColor: .darkGray,
        backgroundImageName: "common_button_white_disable")
    
    // 登录按钮
    lazy var loginButton:UIButton = UIButton.cz_textButton(
        "登录",
        fontSize: 16,
        normalColor: .darkGray,
        highlightedColor: .black,
        backgroundImageName: "common_button_white_disable")
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 根据字典信息来设置访客视图
    func setupInfo(dict:[String:String])
    {
        guard let imageName = dict["imageName"],
                let message = dict["message"]
        else {
            return
        }
        
        if (imageName == "") {
            startAnimate()
            return
        }
        
        tipLabel.text = message
        iconView.isHidden = true
        backIconView.image = UIImage(named: imageName)
    }
    
    func startAnimate()
    {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 10
        anim.repeatCount = MAXFLOAT
        anim.isRemovedOnCompletion = false
        self.backIconView.layer.add(anim, forKey: nil)
    }
}


// MARK: - 设置界面
extension VisitorView
{
    func setupUI()
    {
        backgroundColor = UIColor.white
        
        // 添加视图控件
        addSubview(backIconView)
        addSubview(iconView)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(loginButton)
     
        // 取消autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 自动布局控件
        // backIconView
        addConstraints([NSLayoutConstraint(item: backIconView,
                                           attribute: .centerX,
                                           relatedBy: .equal,
                                           toItem: self,
                                           attribute: .centerX,
                                           multiplier: 1.0,
                                           constant: 0)])
        addConstraints([NSLayoutConstraint(item: backIconView,
                                           attribute: .centerY,
                                           relatedBy: .equal,
                                           toItem: self,
                                           attribute: .centerY,
                                           multiplier: 1.0,
                                           constant: -60)])
        
        // iconView
        addConstraints([NSLayoutConstraint(item: iconView,
                                           attribute: .centerX,
                                           relatedBy: .equal,
                                           toItem: backIconView,
                                           attribute: .centerX,
                                           multiplier: 1.0,
                                           constant: 0)])
        addConstraints([NSLayoutConstraint(item: iconView,
                                           attribute: .centerY,
                                           relatedBy: .equal,
                                           toItem: backIconView,
                                           attribute: .centerY,
                                           multiplier: 1.0,
                                           constant: 0)])
        
        // tipLabel
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: backIconView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        tipLabel.numberOfLines = 2
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .width,
                                         multiplier: 0.7,
                                         constant: 0))
    
        // registerButton
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 30))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: registerButton,
                                         attribute: .height,
                                         multiplier: 3.0,
                                         constant: 0))
        
        // loginButton
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: registerButton,
                                         attribute: .top,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: registerButton,
                                         attribute: .width,
                                         multiplier: 1.0,
                                         constant: 0))
        
    }
}









